/**
 * Created by three on 14-11-4.
 */

'use strict';


$.navAsAjax = true;
/*
 * GLOBAL: Sound Config
 */
$.sound_path = "sound/";
$.sound_on = true;
/*
 * Impacts the responce rate of some of the responsive elements (lower
 * value affects CPU but improves speed)
 */
var throttle_delay = 350,
/*
 * The rate at which the menu expands revealing child elements on click
 */
    menu_speed = 235,
/*
 * Turn on JarvisWidget functionality
 * dependency: js/jarviswidget/jarvis.widget.min.js
 */
    enableJarvisWidgets = true,
/*
 * Warning: Enabling mobile widgets could potentially crash your webApp
 * if you have too many widgets running at once
 * (must have enableJarvisWidgets = true)
 */
    enableMobileWidgets = false,
/*
 * Turn on fast click for mobile devices?
 * Enable this to activate fastclick plugin
 * dependency: js/plugin/fastclick/fastclick.js
 */
    fastClick = false,
/*
 * These elements are ignored during DOM object deletion for ajax version
 * It will delete all objects during page load with these exceptions:
 */
    ignore_key_elms = ["#header, #left-panel, #main, div.page-footer, #shortcut, #divSmallBoxes, #divMiniIcons, #divbigBoxes, #voiceModal, script"],
/*
 * VOICE COMMAND CONFIG
 * dependency: voicecommand.js
 */
    voice_command = true,
/*
 * Turns on speech without asking
 */
    voice_command_auto = false,
/*
 * 	Sets the language to the default 'en-US'. (supports over 50 languages
 * 	by google)
 *
 *  Afrikaans         ['af-ZA']
 *  Bahasa Indonesia  ['id-ID']
 *  Bahasa Melayu     ['ms-MY']
 *  Català            ['ca-ES']
 *  Čeština           ['cs-CZ']
 *  Deutsch           ['de-DE']
 *  English           ['en-AU', 'Australia']
 *                    ['en-CA', 'Canada']
 *                    ['en-IN', 'India']
 *                    ['en-NZ', 'New Zealand']
 *                    ['en-ZA', 'South Africa']
 *                    ['en-GB', 'United Kingdom']
 *                    ['en-US', 'United States']
 *  Español           ['es-AR', 'Argentina']
 *                    ['es-BO', 'Bolivia']
 *                    ['es-CL', 'Chile']
 *                    ['es-CO', 'Colombia']
 *                    ['es-CR', 'Costa Rica']
 *                    ['es-EC', 'Ecuador']
 *                    ['es-SV', 'El Salvador']
 *                    ['es-ES', 'España']
 *                    ['es-US', 'Estados Unidos']
 *                    ['es-GT', 'Guatemala']
 *                    ['es-HN', 'Honduras']
 *                    ['es-MX', 'México']
 *                    ['es-NI', 'Nicaragua']
 *                    ['es-PA', 'Panamá']
 *                    ['es-PY', 'Paraguay']
 *                    ['es-PE', 'Perú']
 *                    ['es-PR', 'Puerto Rico']
 *                    ['es-DO', 'República Dominicana']
 *                    ['es-UY', 'Uruguay']
 *                    ['es-VE', 'Venezuela']
 *  Euskara           ['eu-ES']
 *  Français          ['fr-FR']
 *  Galego            ['gl-ES']
 *  Hrvatski          ['hr_HR']
 *  IsiZulu           ['zu-ZA']
 *  Íslenska          ['is-IS']
 *  Italiano          ['it-IT', 'Italia']
 *                    ['it-CH', 'Svizzera']
 *  Magyar            ['hu-HU']
 *  Nederlands        ['nl-NL']
 *  Norsk bokmål      ['nb-NO']
 *  Polski            ['pl-PL']
 *  Português         ['pt-BR', 'Brasil']
 *                    ['pt-PT', 'Portugal']
 *  Română            ['ro-RO']
 *  Slovenčina        ['sk-SK']
 *  Suomi             ['fi-FI']
 *  Svenska           ['sv-SE']
 *  Türkçe            ['tr-TR']
 *  български         ['bg-BG']
 *  Pусский           ['ru-RU']
 *  Српски            ['sr-RS']
 *  한국어          ['ko-KR']
 *  中文            'cmn-Hans-CN', '普通话 (中国大陆)']
 *                    ['cmn-Hans-HK', '普通话 (香港)']
 *                    ['cmn-Hant-TW', '中文 (台灣)']
 *                    ['yue-Hant-HK', '粵語 (香港)']
 *  日本語                         ['ja-JP']
 *  Lingua latīna     ['la']
 */
    voice_command_lang = 'en-US',
/*
 * 	Use localstorage to remember on/off (best used with HTML Version)
 */
    voice_localStorage = false;
/*
 * Voice Commands
 * Defines all voice command variables and functions
 */
if (voice_command) {

    var commands = {

        'show dashboard' : function() { window.location.hash = "dashboard" },
        'show inbox' : function() {  window.location.hash = "inbox" },
        'show graphs' : function() {  window.location.hash = "graphs/flot" },
        'show flotchart' : function() { window.location.hash = "graphs/flot" },
        'show morris chart' : function() { window.location.hash = "graphs/morris" },
        'show inline chart' : function() { window.location.hash = "graphs/inline-charts" },
        'show dygraphs' : function() { window.location.hash = "graphs/dygraphs" },
        'show tables' : function() { window.location.hash = "tables/table" },
        'show data table' : function() { window.location.hash = "tables/datatable" },
        'show jquery grid' : function() { window.location.hash = "tables/jqgrid" },
        'show form' : function() { window.location.hash = "forms/form-elements" },
        'show form layouts' : function() { window.location.hash = "forms/form-templates" },
        'show form validation' : function() { window.location.hash = "forms/validation" },
        'show form elements' : function() { window.location.hash = "forms/bootstrap-forms" },
        'show form plugins' : function() { window.location.hash = "forms/plugins" },
        'show form wizards' : function() { window.location.hash = "forms/wizards" },
        'show bootstrap editor' : function() { window.location.hash = "forms/other-editors" },
        'show dropzone' : function() { window.location.hash = "forms/dropzone" },
        'show image cropping' : function() { window.location.hash = "forms/image-editor" },
        'show general elements' : function() { window.location.hash = "ui/general-elements" },
        'show buttons' : function() { window.location.hash = "ui/buttons" },
        'show fontawesome' : function() { window.location.hash = "ui/icons/fa" },
        'show glyph icons' : function() { window.location.hash = "ui/icons/glyph" },
        'show flags' : function() { window.location.hash = "ui/icons/flags" },
        'show grid' : function() { window.location.hash = "ui/grid" },
        'show tree view' : function() { window.location.hash = "ui/treeview" },
        'show nestable lists' : function() { window.location.hash = "ui/nestable-list" },
        'show jquery U I' : function() { window.location.hash = "ui/jqui" },
        'show typography' : function() { window.location.hash = "ui/typography" },
        'show calendar' : function() { window.location.hash = "calendar" },
        'show widgets' : function() { window.location.hash = "widgets" },
        'show gallery' : function() { window.location.hash = "gallery" },
        'show maps' : function() { window.location.hash = "gmap-xml" },
        'go back' :  function() { history.back(1); },
        'scroll up' : function () { $('html, body').animate({ scrollTop: 0 }, 100); },
        'scroll down' : function () { $('html, body').animate({ scrollTop: $(document).height() }, 100);},
        'hide navigation' : function() {
            if ($.root_.hasClass("container") && !$.root_.hasClass("menu-on-top")){
                $('span.minifyme').trigger("click");
            } else {
                $('#hide-menu > span > a').trigger("click");
            }
        },
        'show navigation' : function() {
            if ($.root_.hasClass("container") && !$.root_.hasClass("menu-on-top")){
                $('span.minifyme').trigger("click");
            } else {
                $('#hide-menu > span > a').trigger("click");
            }
        },
        'mute' : function() {
            $.sound_on = false;
            $.smallBox({
                title : "MUTE",
                content : "All sounds have been muted!",
                color : "#a90329",
                timeout: 4000,
                icon : "fa fa-volume-off"
            });
        },
        'sound on' : function() {
            $.sound_on = true;
            $.speechApp.playConfirmation();
            $.smallBox({
                title : "UNMUTE",
                content : "All sounds have been turned on!",
                color : "#40ac2b",
                sound_file: 'voice_alert',
                timeout: 5000,
                icon : "fa fa-volume-up"
            });
        },
        'stop' : function() {
            smartSpeechRecognition.abort();
            $.root_.removeClass("voice-command-active");
            $.smallBox({
                title : "VOICE COMMAND OFF",
                content : "Your voice commands has been successfully turned off. Click on the <i class='fa fa-microphone fa-lg fa-fw'></i> icon to turn it back on.",
                color : "#40ac2b",
                sound_file: 'voice_off',
                timeout: 8000,
                icon : "fa fa-microphone-slash"
            });
            if ($('#speech-btn .popover').is(':visible')) {
                $('#speech-btn .popover').fadeOut(250);
            }
        },
        'help' : function() {
            $('#voiceModal').removeData('modal').modal( { remote: "", show: true } );
            if ($('#speech-btn .popover').is(':visible')) {
                $('#speech-btn .popover').fadeOut(250);
            }
        },
        'got it' : function() {
            $('#voiceModal').modal('hide');
        },
        'logout' : function() {
            $.speechApp.stop();
            window.location = $('#logout > span > a').attr("href");
        }
    };

};
/*
 * END APP.CONFIG
 */

$.root_ = $("body");
$.intervalArr = [];
var calc_navbar_height = function () {
        var a = null;
        if ($("#header").length) {
            a = $("#header").height()
        }
        if (a === null) {
            a = $('<div id="header"></div>').height()
        }
        if (a === null) {
            return 49
        }
        return a
    },
    navbar_height = calc_navbar_height,
    shortcut_dropdown = $("#shortcut"),
    bread_crumb = $("#ribbon ol.breadcrumb"),
    topmenu = false,
    thisDevice = null,
    jsArray = {},
    device = null,
    ismobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));

if (!ismobile) {
    $.root_.addClass("desktop-detected");
    device = "desktop"
} else {
    $.root_.addClass("mobile-detected");
    device = "mobile";
    if (fastClick) {
        $.root_.addClass("needsclick");
        FastClick.attach(document.body)
    }
}
if ($("body").hasClass("menu-on-top") || topmenu==true || localStorage.getItem("sm-setmenu") == "top") {
    topmenu = true;
    $("body").addClass("menu-on-top")
}
jQuery(document).ready(function () {
    if ($("[rel=tooltip]").length) {
        $("[rel=tooltip]").tooltip()
    }
    $(document).mouseup(function (a) {
        if (!$(".ajax-dropdown").is(a.target) && $(".ajax-dropdown").has(a.target).length === 0) {
            $(".ajax-dropdown").fadeOut(150);
            $(".ajax-dropdown").prev().removeClass("active")
        }
    });
    $("button[data-btn-loading]").on("click", function () {
        var a = $(this);
        a.button("loading");
        setTimeout(function () {
            a.button("reset")
        }, 3000)
    })
});
// 定义 resize事件处理
(function (jquery, _this, c) {
    var array = jquery([]),
        resizeConfig = jquery.resize = jquery.extend(jquery.resize, {}),
        j,
        setTimeout = "setTimeout",
        resize = "resize",
        resizeEvent = resize + "-special-event",
        delay = "delay",
        throttleWindow = "throttleWindow";
    resizeConfig[delay] = throttle_delay;
    resizeConfig[throttleWindow] = true;
    jquery.event.special[resize] = {
        setup: function () {
            if (!resizeConfig[throttleWindow] && this[setTimeout]) {
                return false
            }
            var m = jquery(this);
            array = array.add(m);
            try {
                jquery.data(this, resizeEvent, {w: m.width(), h: m.height()})
            } catch (n) {
                jquery.data(this, resizeEvent, {w: m.width, h: m.height})
            }
            if (array.length === 1) {
                h()
            }
        },
        teardown: function () {
            if (!resizeConfig[throttleWindow] && this[setTimeout]) {
                return false
            }
            var m = jquery(this);
            array = array.not(m);
            m.removeData(resizeEvent);
            if (!array.length) {
                clearTimeout(j)
            }
        },
        add: function (m) {
            if (!resizeConfig[throttleWindow] && this[setTimeout]) {
                return false
            }
            var o;

            function n(t, p, q) {
                var r = jquery(this), s = jquery.data(this, resizeEvent);
                s.w = p !== c ? p : r.width();
                s.h = q !== c ? q : r.height();
                o.apply(this, arguments)
            }

            if (jquery.isFunction(m)) {
                o = m;
                return n
            } else {
                o = m.handler;
                m.handler = n
            }
        }
    };
    function h() {
        j = _this[setTimeout](function () {
            array.each(function () {
                var n;
                var m;
                var o = jquery(this), p = jquery.data(this, resizeEvent);
                try {
                    n = o.width()
                } catch (q) {
                    n = o.width
                }
                try {
                    m = o.height()
                } catch (q) {
                    m = o.height
                }
                if (n !== p.w || m !== p.h) {
                    o.trigger(resize, [p.w = n, p.h = m])
                }
            });
            h()
        }, resizeConfig[delay])
    }
})(jQuery, this);

$("#main").resize(function () {
    check_if_mobile_width()
});
function check_if_mobile_width() {
    if ($(window).width() < 979) {
        $.root_.addClass("mobile-view-activated");
        $.root_.removeClass("minified")
    } else {
        if ($.root_.hasClass("mobile-view-activated")) {
            $.root_.removeClass("mobile-view-activated")
        }
    }
}
var ie = (function () {
    var c, a = 3, d = document.createElement("div"), b = d.getElementsByTagName("i");
    while (d.innerHTML = "<!--[if gt IE " + (++a) + "]><i></i><![endif]-->", b[0]) {
    }
    return a > 4 ? a : c;
}());

$.fn.extend({jarvismenu: function (a) {
    var d = {accordion: "true", speed: 200, closedSign: "[+]", openedSign: "[-]"};
    var b = $.extend(d, a);
    var c = $(this);
    c.find("li[init!=init]").each(function () {
        if ($(this).find("ul").size() !== 0) {
            $(this).find("a:first").append("<b class='collapse-sign'>" + b.closedSign + "</b>");
            if ($(this).find("a:first").attr("href") == "#" || $(this).find("a:first").attr("href") == "") {
                $(this).find("a:first").attr("href", "javascript:void(0)");
                $(this).find("a:first").click(function () {
                    return false
                })
            }
        }
    });
    c.find("li[init!=init].active").each(function () {
        $(this).parents("ul").slideDown(b.speed);
        $(this).parents("ul").parent("li").find("b:first").html(b.openedSign);
        $(this).parents("ul").parent("li").addClass("open")
    });

    c.find("li[init!=init] a").click(function () {
        if ($(this).parent().find("ul").size() !== 0) {
            if (b.accordion) {
                if (!$(this).parent().find("ul").is(":visible")) {
                    var parents = $(this).parent().parents("ul");
                    var visible = c.find("ul:visible");
                    visible.each(function (e) {
                        var f = true;
                        parents.each(function (g) {
                            if (parents[g] == visible[e]) {
                                f = false;
                                return false
                            }
                        });
                        if (f) {
                            if ($(this).parent().find("ul") != visible[e]) {
                                $(visible[e]).slideUp(b.speed, function () {
                                    $(this).parent("li").find("b:first").html(b.closedSign);
                                    $(this).parent("li").removeClass("open")
                                })
                            }
                        }
                    })
                }
            }
            if ($(this).parent().find("ul:first").is(":visible") && !$(this).parent().find("ul:first").hasClass("active")) {
                $(this).parent().find("ul:first").slideUp(b.speed, function () {
                    $(this).parent("li").removeClass("open");
                    $(this).parent("li").find("b:first").delay(b.speed).html(b.closedSign)
                })
            } else {
                $(this).parent().find("ul:first").slideDown(b.speed, function () {
                    $(this).parent("li").addClass("open");
                    $(this).parent("li").find("b:first").delay(b.speed).html(b.openedSign)
                })
            }
        }
    })
    c.find("li[init!=init]").attr("init", "init")
}});

jQuery.fn.doesExist = function () {
    return jQuery(this).length > 0
};

$("body").on("click", function (a) {
    $('[rel="popover"]').each(function () {
        if (!$(this).is(a.target) && $(this).has(a.target).length === 0 && $(".popover").has(a.target).length === 0) {
            $(this).popover("hide")
        }
    })
});

//var $html = angular.element(document.getElementsByTagName('html')[0]);
/* jshint ignore:end */
angular.element(document).ready(function () {
    angular.bootstrap(document, [app.name])
});
